#include<vector>
#include<iostream>
using std::vector;

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        int count = 0;
        vector<vector<bool>> visited(m, vector<bool>(n, false));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1' && !visited[i][j]) {
                    // discover new island
                    count++;
                    std::cout << "\n";
                    std::cout << "new island (" << i << "," << j << ")\n";
                    std::cout << "\n";
                    search(i, j, grid, visited);
                }
            }
        }
        return count;
    }

    void search(int i, int j, vector<vector<char>>& grid, vector<vector<bool>>& visited) {
        //print(i, j, visited);
        // not island or has been visited, ignore
        if (grid[i][j] == '0' || visited[i][j]) return;
        // clare that island is id
        visited[i][j] = true;
        // search around
        for (int k = 0; k < 4; k++) {
            int x = i + arounds[k][0];
            int y = j + arounds[k][1];
            if (x >= 0 && y >= 0 && x < grid.size() && y < grid[0].size()) {
                search(x, y, grid, visited);
            }
        }
        return;
    }
private:
    vector<vector<int>> arounds = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    void print(int i, int j, vector<vector<bool>>& visited) {
        std::cout << "\n";
        std::cout << "visit grid (" << i << ", " << j << ")\n";
        for (int i = 0; i < visited.size(); i++) {
            for (int j = 0; j < visited.size(); j++)  {
                std::cout << int(visited[i][j]) << " ";
            }
            std::cout << "\n";
        }
        std::cout << "\n";
    }
};


int main() {
    int m;
    int n;
    char tmp;
    std::cin >> m >> n;
    vector<vector<char>> islands(m, vector<char>(n, '0'));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> tmp;
            islands[i][j] = tmp;
        }
    }
    std::cout << "island: \n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << islands[i][j] << " ";
        }
        std::cout << "\n";
    }
    auto res = Solution().numIslands(islands);
    std::cout << "Num of islands: " << res << "\n";
    return 0;

}

